<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/layui/lib/layui-v2.5.5/css/layui.css"
          media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/layui/css/public.css" media="all">

    <style>
        .thumbBox {
            height: 200px;
            overflow: hidden;
            border: 1px solid #e6e6e6;
            border-radius: 2px;
            cursor: pointer;
            position: relative;
            text-align: center;
            line-height: 200px;
            width: 210px;
        }

        .thumbImg {
            max-width: 100%;
            max-height: 100%;
            border: none;
        }

        .thumbBox:after {
            position: absolute;
            width: 100%;
            height: 100%;
            line-height: 200px;
            z-index: -1;
            text-align: center;
            font-size: 20px;
            content: "缩略图";
            left: 0;
            top: 0;
            color: #9F9F9F;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">企业名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="e_name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">账号状态</label>
                            <div class="layui-input-inline">
                                <select name="e_state">
                                    <option value="" selected="">-----</option>
                                    <option value="审核中">审核中</option>
                                    <option value="通过">通过</option>
                                    <option value="不通过">不通过</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn" lay-submit lay-filter="data-search-btn"><i
                                    class="layui-icon layui-icon-search"></i>搜索
                            </button>
                            <button type="reset" class="layui-btn layui-btn-warm"><i
                                    class="layui-icon layui-icon-refresh-1"></i>重置
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add">
                    <i class="layui-icon layui-icon-add-1"></i> 添加
                </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete">
                    <i class="layui-icon layui-icon-delete"></i>删除
                </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

        <%-- 添加和修改窗口 --%>
        <div style="display: none;padding: 5px" id="addOrUpdateWindow">
            <form class="layui-form" style="width:90%;" id="dataFrm" lay-filter="dataFrm">

                <div class="layui-col-md12 layui-col-xs12">
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-md9 layui-col-xs7">
                            <div class="layui-form-item magt3" style="margin-top: 8px;">
                                <label class="layui-form-label">账号ID</label>
                                <div class="layui-input-block">
                                    <input type="text" name="e_id" lay-verify="required" autocomplete="off"
                                           placeholder="请输入账号ID：（如：e10010）" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">企业类型</label>
                                <div class="layui-input-block">
                                    <select name="e_type" id="e_type" lay-verify="required">
                                        <option value="">请选择企业类型</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">账号名称</label>
                                <div class="layui-input-block">
                                    <input type="text" name="e_name" lay-verify="required" autocomplete="off"
                                           placeholder="请输入账号名称"
                                           class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">账号状态</label>
                                <div class="layui-input-inline">
                                    <select name="e_state">
                                        <option value="审核中" selected="">审核中</option>
                                        <option value="通过">通过</option>
                                        <option value="不通过">不通过</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-md3 layui-col-xs5">
                            <div class="layui-upload-list thumbBox mag0 magt3">
                                <input type="hidden" name="e_photo" id="e_photo"
                                       value="/statics/images/defaultimg.jpg">
                                <img class="layui-upload-img thumbImg"
                                     src="/statics/images/defaultimg.jpg">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item magb0">
                        <label class="layui-form-label">地址</label>
                        <div class="layui-input-block">
                            <input type="text" name="e_address" lay-verify="required" autocomplete="off"
                                   placeholder="请输入企业地址"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">企业创始人</label>
                        <div class="layui-input-block">
                            <input type="text" name="e_principal" lay-verify="required" autocomplete="off"
                                   placeholder="请输入企业创始人"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">联系电话</label>
                        <div class="layui-input-block">
                            <input type="text" name="e_phone" lay-verify="required" autocomplete="off" maxlength="11"
                                   placeholder="如：134*****xxxx"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">联系邮箱</label>
                        <div class="layui-input-block">
                            <input type="text" name="e_email" lay-verify="required" autocomplete="off"
                                   placeholder="如：sjfsijfseh@163.com"
                                   class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">企业简介</label>
                        <div class="layui-input-block">
                            <textarea class="layui-textarea" name="e_intro" id="e_intro"></textarea>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">企业文化</label>
                        <div class="layui-input-block">
                            <textarea class="layui-textarea" name="e_culture" id="e_culture"
                                      style="margin-top: 20px;"></textarea>
                        </div>
                    </div>

                    <div class="layui-form-item layui-row layui-col-xs12">
                        <div class="layui-input-block" style="text-align: center;">
                            <button type="button" class="layui-btn" lay-submit lay-filter="doSubmit"><span
                                    class="layui-icon layui-icon-add-1"></span>提交
                            </button>
                            <button type="reset" class="layui-btn layui-btn-warm">
                                <span class="layui-icon layui-icon-refresh-1"></span>重置
                            </button>
                        </div>
                    </div>
                </div>


            </form>
        </div>


    </div>
</div>
<script src="${pageContext.request.contextPath}/statics/layui/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'upload', 'layer','layedit'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            upload = layui.upload,
            layer = layui.layer,
            layedit = layui.layedit;

        var tableIns = table.render({
            elem: '#currentTableId',
            url: '${pageContext.request.contextPath}/admin/user/enterprise/list?check=0',
            toolbar: '#toolbarDemo',

            cols: [[
                {type: "checkbox", width: 50},
                {field: 'e_id', width: 120, title: '登录账号', align: 'center', sort: true},
                {field: 'e_name', minWidth: 120, title: '名称', align: 'center', sort: true},
                {field: 'e_type', minWidth: 120, title: '类型', align: 'center', sort: true},
                {field: 'e_principal', minWidth: 120, title: '创建人', align: 'center'},
                {field: 'e_state', minWidth: 120, title: '状态', align: 'center'},
                {title: '操作', minWidth: 120, toolbar: '#currentTableBar', align: "center"}
            ]],
            page: true,
            done: function (res, curr, count) {
                //判断当前页码是否是大于1并且当前页的数据量为0
                if (curr > 1 && res.data.length == 0) {
                    var pageValue = curr - 1;
                    //刷新数据表格的数据
                    tableIns.reload({
                        page: {curr: pageValue}
                    });
                }
            }

        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            tableIns.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                mainIndex = layer.open({
                    type: 1,//打开类型
                    title: "添加企业信息",//窗口标题
                    area: ["800px", "500px"],//窗口宽高
                    content: $("#addOrUpdateWindow"),//引用的内容窗口
                    success: function () {
                        //清空表单数据
                        $("#dataFrm")[0].reset();
                        //添加的提交请求
                        url = "/admin/user/enterprise/addEnterprise";
                        //重置默认图片,注意：显示图片必须在图片名称前加上/hotel/show
                        $(".thumbImg").attr("src","/statics/images/defaultimg.jpg");
                        //重置图片隐藏域的值
                        $("#photo").val("/statics/images/defaultimg.jpg");
                    }
                });

            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        //监听表单提交事件
        form.on("submit(doSubmit)", function (data) {
            $.post(url, data.field, function (result) {
                if (result.success) {
                    //刷新数据表格
                    tableIns.reload();
                    //关闭窗口
                    layer.close(mainIndex);
                }
                //提示信息
                layer.msg(result.message);
            }, "json");
            //禁止页面刷新
            return false;
        });


        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                mainIndex = layer.open({
                    type: 1,//打开类型
                    title: "修改企业账号",//窗口标题
                    area: ["800px", "500px"],//窗口宽高
                    content: $("#addOrUpdateWindow"),//引用的内容窗口
                    success: function () {
                        //表单数据回显
                        form.val("dataFrm", data);//参数1：lay-filter值  参数2：回显的数据
                        //修改的提交请求
                        url = "/admin/user/enterprise/updateEnterprise";
                        //图片回显
                        $(".thumbImg").attr("src","/hotel/show/"+data.e_photo);
                        //图片隐藏域的值回显
                        $("#photo").val(data.e_photo);
                    }
                });
            } else if (obj.event === 'delete') {
                //判断当前企业下是否存在真正招聘的工作
                $.get("/admin/user/enterprise/checkEnterpriseHasEmployment", {"e_id": data.e_id}, function (result) {
                    if (result.exist) {
                        //提示无法删除
                        layer.msg(result.message);
                    } else {
                        //提示用户是否删除该企业账号
                        layer.confirm("确定要删除[<font color='red'>" + data.e_name + "</font>]吗", {
                            icon: 3,
                            title: '提示'
                        }, function (index) {
                            //发送ajax请求进行删除
                            $.post("/admin/user/enterprise/deleteById", {"e_id": data.e_id}, function (result) {
                                if (result.success) {
                                    //刷新数据表格
                                    tableIns.reload();
                                }
                                //提示
                                layer.msg(result.message);
                            }, "json");

                            layer.close(index);
                        });
                    }
                }, "json");

            }
        });


        //查询企业类型下拉列表
        $.get("/Type/findEnterpriseTypeAll", function (result) {
            var html = "";
            for (var i = 0; i < result.length; i++) {
                html += "<option value='" + result[i].name + "'>" + result[i].name + "</option>"
            }
            //将网页代码追加到下拉列表中
            $("[name='e_type']").append(html);
            //重新渲染下拉列表
            form.render("select");
        }, "json");


        //渲染文件上传区域
        upload.render({
            elem: ".thumbImg",//绑定元素
            url: '/file/uploadFile',//文件上传地址
            acceptMime: 'image/*',//规定打开文件选择框时，筛选出的文件类型
            field: 'file',//文件上传的字段值，等同于input标签的name属性值，该值必须与控制器中的方法参数名一致
            method: "post",//提交方式
            //文件上传成功后的回调函数
            done: function (res, index, upload) {
                //设置图片回显路径
                $(".thumbImg").attr("src", res.data.src);
                $('.thumbBox').css("background", "#fff");
                console.log(res.msg)
                //给图片隐藏域赋值
                $("#e_photo").val(res.imagePath);
            }
        });


    });
</script>

</body>
</html>
